#!/usr/bin/env python
#-*-coding:utf-8-*-
from  urllib import  parse,request
from  http import cookiejar
import  re
url='http://www.xicidaili.com/nn/'
urll='http://www.baidu.com'
head = {}
List=[]

head['User-Agent']=r'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko'
req=request.Request(url=url,headers=head)
req1=request.Request(url=urll,headers=head)
def ce(ipp):
    proxy=request.ProxyHandler({"http":ipp})
    opener=request.build_opener(proxy)
    try:
        s=opener.open(req1,timeout=2)
        #print(s.read())
        return False
    except:
        return True
def get_ip():
    html = request.urlopen(req).read().decode('utf-8')
    # print(html)
    ip = re.findall('<td>([0-9]{2,3}\.[0-9]{2,3}\.[0-9]{2,3}\.[0-9]{2,3})</td>[\\s]+<td>([0-9]+)<', html)
    print(ip)
    for i in ip:
        s=i[0]+":"+i[1]
        List.append(i[0]+":"+i[1])
        #print(s)
def get_():
    if (len(List)<=0) :
        get_ip()
        s=List[0]
        List.pop(0)
        i=1
        while ce(s):
           # print(s+" "+str(i))
            i=i+1
            if(len(List)<=0):
                get_ip()
            s = List[0]
            List.pop(0)
        return s
    else:
        s = List[0]
        List.pop(0)
        while ce(s):
           if(len(List)<=0):
              get_ip()

           s = List[0]
           List.pop(0)
           #print(s)
        return s
if __name__=='__main__':
    print("\t\t\t"+get_())
    print("\t\t\t"+get_())













